<template>
	<view>
        <button @click="open">创建作品集</button>
        <uni-popup ref="addFrom" type="top" class="popup">
        <view class="popup_x">
		<uni-forms ref="form" :modelValue="formData"  :rules="rules" label-width="93">
			<uni-forms-item label="作品名称" name="name" required>
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入作品名称" />
			</uni-forms-item>
            <uni-forms-item label="作品介绍" name="introduce">
				<uni-easyinput type="text" v-model="formData.introduce" placeholder="请输入作品介绍" />
			</uni-forms-item>
            <uni-forms-item label="作品类型" name="type">
				<uni-easyinput type="text" v-model="formData.type" placeholder="请输入作品类型" />
			</uni-forms-item>
		</uni-forms>
		<button class="button" @click="submit">创建作品集</button>
		</view>
    </uni-popup>
	</view>
</template>

<script>
import {createMyCreation} from '@/api/mycreation'
export default {
  name: 'addFrom',
  props:{
    author:{
    type:String,
    required:true
    }
  },

  data() {
    return {
        formData:{
            "author": null,//作者账号
            "name": null,//作品名称
            "introduce": null,//作品介绍
            "time":null,//创建时间(秒级时间戳)
            "type":null,//作品类型
        },//表单所填的数据
        rules: {
            name: {
                rules: [{
                    required: true,
                    errorMessage: '姓名不能为空'
                }]
            }
			},
    };
  },
  onReady() {
		// 需要在onReady中设置规则
		this.$refs.form.setRules(this.rules)
	},
  mounted() {
    
  },

  methods: {

    open(){
        this.$refs.addFrom.open()
    },
    close() {
			this.$refs.addFrom.close()
		},


    submit(form) {
			// console.log(form);
            console.log(this.formData);
            console.log(this.author);

        let author=this.author
        const timestamp =Math.floor( Date.now()/1000);
        this.formData.author=author;
        this.formData.time=timestamp;
            if (this.formData.name) {
                createMyCreation(this.formData).then(res=>{
                    console.log(res);
                    uni.showToast({
                        title:"创建成功",
                        icon:'success'
                    })
                   
                    // 在此处调用父组件方法
                    this.$emit("needFresh")
                   
                }).catch(err=>{
                    uni.showToast({
                        title:"创建失败",
                        icon:'error'
                    })
                })
                 this.close()
            }else{

                uni.showToast({
                        title:"请填写作品名称",
                        icon:'error'
                    })   
            }
         
		}
	
  },
};
</script>
<style>
.popup_x{
    /* width: 90%; */
    /* height: 80%; */
    width: 100%;
    background-color: #fff;
    padding: 20upx;
    padding-bottom: 40upx;
    /* border-radius: 10px; */
}
.button{
    border: 2upx solid #ccc;
}

</style>